package com.wwwarehouse.contract.adjust_contract;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.contract.bean.ConstractOrderDetailsBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * Creator by zhenwang.xiang on 2017/10/18 15:05
 * Description: 商品明细
 */


public class ConstractOrderDetailsFragment extends BaseFragment implements View.OnClickListener {
    private ImageView mImgPhoto;
    private TextView mName, mType, mCount, mOrderTime, mOrderState, mOrderNumber;
    private TextView mWeekPay, mOrderPay, mBankCardPay, mAliPay, mWechatPay, mOfflinePay;
    private TextView mTotalMoney, mAddress, mContant;
    private Button mBtnConfrim, mBtnTermationOrder, mCheckOrderGoods;
    private int mFlag = 0;
    private String businessId;
    private ArrayList<String> mBusinessIdList;
    private String contractUkid;
    private ConstractOrderDetailsBean mOrderDetailsBean;
    private StateLayout mStateLayout;
    private View mSpaceOne;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_contract_order_details, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        if (getArguments() != null) {
            mFlag = getArguments().getInt("flag", 0);
            if (mFlag == 1) {
                String stopId = getArguments().getString("stopId");
                contractUkid = getArguments().getString("contractUkid");
                if (!TextUtils.isEmpty(stopId)) {
                    mBusinessIdList.add(stopId);
                } else {
                    mBusinessIdList = getArguments().getStringArrayList("businessIds");
                }
            } else {
                contractUkid = getArguments().getString("contractUkid");
                businessId = getArguments().getString("businessId");
                mBusinessIdList.add(businessId);
            }

        }

        //mFlag==1是信息卡片跳转过来的
        if (mFlag == 1) {
            mBtnTermationOrder.setVisibility(View.GONE);
            mBtnConfrim.setVisibility(View.VISIBLE);
        } else {
            mBtnTermationOrder.setVisibility(View.VISIBLE);
            mBtnConfrim.setVisibility(View.GONE);
        }
    }

    private void initView() {
        mImgPhoto = $(R.id.detial_image);
        mName = $(R.id.detial_name);
        mType = $(R.id.detial_type);
        mCount = $(R.id.detial_count);
        mOrderTime = $(R.id.detial_order_time);
        mWeekPay = $(R.id.detial_weekpay);
        mOrderPay = $(R.id.detial_orderpay);
        mBankCardPay = $(R.id.detial_bankcardpay);
        mAliPay = $(R.id.detial_alipay);
        mWechatPay = $(R.id.detial_wechatpay);
        mOfflinePay = $(R.id.detial_offlinepay);
        mTotalMoney = $(R.id.detial_totalmoney);
        mAddress = $(R.id.detial_address);
        mContant = $(R.id.detial_contant);
        mBtnConfrim = $(R.id.btn_confirm);
        mBtnTermationOrder = $(R.id.btn_terminationorder);
        mCheckOrderGoods = $(R.id.btn_check_ordergoods);
        mStateLayout = $(R.id.state_layout);
        mOrderState = $(R.id.detial_order_state);
        mOrderNumber = $(R.id.detial_order_number);
        mSpaceOne = $(R.id.view_space_one);

        mStateLayout.showProgressView(true);

        mBusinessIdList = new ArrayList<>();

        mBtnConfrim.setOnClickListener(this);
        mBtnTermationOrder.setOnClickListener(this);
        mCheckOrderGoods.setOnClickListener(this);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ConstractOrderDetailsFragment) {
            mActivity.setTitle(getString(R.string.constract_order_detials_title));
        }
    }

    @Override
    public void requestDatas() {
        requestHttp();

    }

    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        map.put("contractUkid", contractUkid);
        map.put("businessIds", mBusinessIdList);

        NoHttpUtils.httpPost(ContractConstant.GET_ORDER_PAYMENT_DETAIL, map, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if ("0".equals(commonClass.getCode())) {
                        mOrderDetailsBean = JSON.parseObject(commonClass.getData().toString(), ConstractOrderDetailsBean.class);
                        if (mOrderDetailsBean != null) {
                            setData(mOrderDetailsBean);
                        }
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                requestHttp();
                            }
                        });
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mStateLayout.showSystemView(result, true);
            mStateLayout.setSystemListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    requestHttp();
                }
            });
        }

        @Override
        public void onFinish(int what) {
            mStateLayout.showContentView();
        }
    };

    private void setData(ConstractOrderDetailsBean orderDetailsBean) {
        ImageloaderUtils.displayImg(ImageloaderUtils.getImageLoader(), orderDetailsBean.getLogoUrl(), mImgPhoto);
        mName.setText(orderDetailsBean.getBusinessName());
        mType.setText("商品种类：" + orderDetailsBean.getSkuCount());
        mCount.setText("商品件数：" + orderDetailsBean.getItemCount());
        mOrderTime.setText("下单时间：" + orderDetailsBean.getContractTime());
        mOrderNumber.setText("订单号：" + orderDetailsBean.getContractId());
        if ("10".equals(orderDetailsBean.getOrderStatus())) {
            mOrderState.setText("待付款");
        } else if ("440".equals(orderDetailsBean.getOrderStatus())) {
            mOrderState.setText("待发货");
        } else if ("450".equals(orderDetailsBean.getOrderStatus())) {
            mOrderState.setText("待收货");
        } else if ("500".equals(orderDetailsBean.getOrderStatus())) {
            mOrderState.setText("交易完成");
            mOrderState.setTextColor(getResources().getColor(R.color.common_color_c13_23c0b1));
        } else if ("900".equals(orderDetailsBean.getOrderStatus())) {
            mOrderState.setText("交易关闭");
            mOrderState.setTextColor(getResources().getColor(R.color.common_color_c7_96999e));
        }

        if (0 == orderDetailsBean.getPeriodPay()) {
            mWeekPay.setVisibility(View.GONE);
            mSpaceOne.setVisibility(View.GONE);
        } else {
            mWeekPay.setText(Html.fromHtml("周期付金额：<font color='#337CFF'>" + orderDetailsBean.getPeriodPay() + "元</font>"));
            mSpaceOne.setVisibility(View.VISIBLE);
        }
        if (0 == orderDetailsBean.getOrderPay()) {
            mOrderPay.setVisibility(View.GONE);
            mSpaceOne.setVisibility(View.GONE);
        } else {
            mOrderPay.setText(Html.fromHtml("下单付金额：<font color='#337CFF'>" + orderDetailsBean.getOrderPay() + "元</font>"));
        }
        if (0 == orderDetailsBean.getBank()) {
            mBankCardPay.setVisibility(View.GONE);
        } else {
            mBankCardPay.setText("银行卡支付：" + orderDetailsBean.getBank() + "元");
        }
        if (0 == orderDetailsBean.getAliPay()) {
            mAliPay.setVisibility(View.GONE);
        } else {
            mAliPay.setText("支付宝支付：" + orderDetailsBean.getAliPay() + "元");
        }

        if (0 == orderDetailsBean.getWechatPay()) {
            mWechatPay.setVisibility(View.GONE);
        } else {
            mWechatPay.setText("微信支付：" + orderDetailsBean.getWechatPay() + "元");
        }

        if (0 == orderDetailsBean.getOffline()) {
            mOfflinePay.setVisibility(View.GONE);
        } else {
            mOfflinePay.setText("线下支付：" + orderDetailsBean.getOffline() + "元");
        }

        mTotalMoney.setText(Html.fromHtml("合计：<font color='#337CFF'>" + orderDetailsBean.getTotalPrice() + "元</font>"));
        mAddress.setText("配送地址：" + orderDetailsBean.getAddress());
        String contant = "";
        if (!TextUtils.isEmpty(orderDetailsBean.getReceiveName())) {
            contant = "联系人：" + orderDetailsBean.getReceiveName() + "\t\t";
        }
        if (!TextUtils.isEmpty(orderDetailsBean.getMobilePhone())) {
            contant += orderDetailsBean.getMobilePhone() + "\t";
        }

        mContant.setText(contant);

    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_terminationorder) {
            SecondaryConfirmationFragment confirmationFragment = new SecondaryConfirmationFragment();
            Bundle bundle = new Bundle();
            bundle.putString("businessId", businessId);
            bundle.putString("contractUkid", contractUkid);
            confirmationFragment.setArguments(bundle);
            pushFragment(confirmationFragment);

        } else if (i == R.id.btn_check_ordergoods) {
            ConstractProductDetailsViewPagerFragment productDetailsViewPagerFragment = new ConstractProductDetailsViewPagerFragment();
            Bundle bundle = new Bundle();
            bundle.putParcelable("orderbean", mOrderDetailsBean);
            bundle.putString("contractUkid", contractUkid);
            productDetailsViewPagerFragment.setArguments(bundle);
            pushFragment(productDetailsViewPagerFragment);
        } else if (i == R.id.btn_confirm) {
            popFragment();
        }
    }
}
